package com.mocha.websocket;

import com.alibaba.fastjson.JSON;
import com.mocha.websocket.metadata.BitMessage;
import com.mocha.websocket.metadata.MochaBasicBinaryMessage;
import com.mocha.websocket.metadata.MochaBinaryProtocol;
import com.mocha.common.core.MochaWSResponse;
import com.mocha.common.core.ResponseCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.Assert;

import javax.websocket.Session;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author: songkui
 * @since: 2023/2/21 17:06
 */
@Slf4j
public class SessionManager {


    private static ConcurrentHashMap<Long, Session> sessionMap = new ConcurrentHashMap<>();
    private static ConcurrentHashMap<String, Long> sessionIdMap = new ConcurrentHashMap<>();


    public static void putSession(Long userId, Session session) {
        sessionMap.put(userId, session);
        sessionIdMap.put(session.getId(), userId);
    }

    public static void removeSession(Long userId) {
        if (sessionMap.isEmpty() || userId == null) {
            return;
        }
        Session session = sessionMap.get(userId);
        if (null != session) {
            sessionMap.remove(userId);
            sessionIdMap.remove(session.getId());
        }

    }

    public static void removeSession(Session session) {
        if (sessionMap.isEmpty() || null == session) {
            return;
        }
        Long userId = sessionIdMap.get(session.getId());
        if (null != userId) {
            sessionIdMap.remove(session.getId());
            sessionMap.remove(userId);
        }
    }

    public static Long getKey(Session session) {
        return session == null ? null : sessionIdMap.get(session.getId());
    }

    public static Session getSession(Long userId) {
        return sessionMap.get(userId);
    }


    //  true : 表示 没有该session   false ： 表示有该session
    public static boolean validSession(Session session) {
        if (null == session) {
            return true;
        }
        Long userId = sessionIdMap.get(session.getId());
        if (userId == null) {
            return true;
        }
        Session mapSession = sessionMap.get(userId);
        return null == mapSession ? true : false;
    }


    // ---------------send-msg---------

    public static void sendText(Session session, String message) {
        session.getAsyncRemote().sendText(message);
    }

    public static void sendByteBuffer(Session session, ByteBuffer data) {
        session.getAsyncRemote().sendBinary(data);
    }

    public static void sendByteBuffer(Long userId, ByteBuffer data) {
        sendByteBuffer(sessionMap.get(userId), data);
    }


    public static void sendText(Long userId, String message) {
        sendText(sessionMap.get(userId), message);
    }

    public static <T> void sendMessage(Session session, MochaWSResponse<T> mochaResponse) {
        Assert.notNull(session, "session is null");
        Assert.notNull(mochaResponse, "mochaResponse is null");
        String msg = JSON.toJSONString(mochaResponse);
        sendText(session, msg);
    }

    public static <T> void sendMessage(Long userId, MochaWSResponse<T> mochaResponse) {
        Assert.notNull(userId, "userId is null");
        Assert.notNull(mochaResponse, "mochaResponse is null");
        sendMessage(sessionMap.get(userId), mochaResponse);
    }


    public static <T> void sendMessage(List<Long> idList, MochaWSResponse<T> mochaResponse) {
        if (null == idList || idList.isEmpty()) {
            return;
        }

        for (Long userId : idList) {
            sendMessage(userId, mochaResponse);
        }

    }

    public static <T> void sendMessageByAll(MochaWSResponse<T> mochaResponse) {
        Collection<Session> sessionCollection = sessionMap.values();
        for (Session session : sessionCollection) {
            sendMessage(session, mochaResponse);
        }
    }


    //------------send-Binary-MSG---------------
    public static void sendStateMessage(Session session, MochaBinaryProtocol binaryProtocol, ResponseCode responseCode) {
        binaryProtocol = null == binaryProtocol ? new MochaBinaryProtocol() : binaryProtocol;
        binaryProtocol.setState(responseCode.code());
        binaryProtocol.setData(BitMessage.build());
        sendMessage(session, binaryProtocol);
    }

    public static void sendMessage(Session session, MochaBinaryProtocol binaryProtocol) {
        sendByteBuffer(session, binaryProtocol.encode());
    }

    public static <T extends MochaBasicBinaryMessage> void sendMessage(Long userId, MochaBinaryProtocol<T> binaryMessage) {
        sendByteBuffer(userId, binaryMessage.encode());
    }

    public static <T extends MochaBasicBinaryMessage> void sendMessage(List<Long> idList, MochaBinaryProtocol<T> binaryMessage) {

        if (null == idList || idList.isEmpty()) {
            return;
        }
        for (Long userId : idList) {
            sendMessage(userId, binaryMessage);
        }
    }

    public static <T extends MochaBasicBinaryMessage> void sendMessageByAll(MochaBinaryProtocol<T> binaryMessage) {
        Collection<Session> sessionCollection = sessionMap.values();
        for (Session session : sessionCollection) {
            sendMessage(session, binaryMessage);
        }
    }


}
